// miniprogram/pages/changeGame/changeGame.js
var util = require('../../utils/common.js');

import Encoder from "../../utils/Encoder";
import Decoder from "../../utils/Decoder";
// //实现参考例子
// var message = "减肥是一件困难的事情";

// //UTF-8转GBK
// var gbkArray = Encoder.hexstrToArray(Encoder.stringToGbkHexstr(message));
// console.log("gbkArray>>" + gbkArray);
// //GBK转UTF-8
// var utfStr = Decoder.GBKHexstrToString(Decoder.binaryarrayToHexstr(gbkArray));
// console.log("utfStr>>" + utfStr);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    gamesList: [],
    time: "",
    choosen_id: "",  // 当前选中的比赛
    title: "",
    host: "",
    group: [],
    // attention: "",
    fileID: "",
    code: "",
    sbd: "",
    sed: ""
  },

  // 点击对应条目选中对应比赛
  choosen: function(e) {
    // console.log(e)
    this.setData({
      choosen_id: e.currentTarget.dataset.id,
      title: e.currentTarget.dataset.title, 
      host: e.currentTarget.dataset.host,
      group: e.currentTarget.dataset.group,
      // attention: e.currentTarget.dataset.attention,
      fileID: e.currentTarget.dataset.fileid,
      code: e.currentTarget.dataset.code,
      sbd: e.currentTarget.dataset.sbd,
      sed: e.currentTarget.dataset.sed,
    })
    // console.log(this.data.group)
  },

  // 点击报名审核跳转到对应页面
  goToSignUpRecord: function () {
    if(this.data.choosen_id == "") {
      wx.showToast({
        title: '请选择比赛',
        image: "/images/unhappy.png"
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/superSignUpRecord/superSignUpRecord?_id=' + that.data.choosen_id
      })
    }
  },

  // 点击查看成绩跳转到对应页面
  goToGradeRecord: function () {
    if (this.data.choosen_id == "") {
      wx.showToast({
        title: '请选择比赛',
        image: "/images/unhappy.png"
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/superCheckGameRecord/superCheckGameRecord?gameId=' + that.data.choosen_id + "&gameTitle=" + that.data.title
      })
    }
  },
  
  // 点击录入成绩进行对应操作
  // goToAddGrade: function() {
  //   if (this.data.choosen_id == "") {
  //     wx.showToast({
  //       title: '请选择比赛',
  //       image: "/images/unhappy.png"
  //     })
  //   } else {
  //     let that = this
  //     // 选择附件上传
  //     wx.chooseMessageFile({
  //       count: 1,
  //       type: 'all',
  //       success(res) {
  //         console.log("选择成绩附件成功", res)
  //         wx.cloud.uploadFile({
  //           cloudPath: res.tempFiles[0].name,
  //           filePath: res.tempFiles[0].path,
  //           success: res => {
  //             console.log("上传成绩附件成功", res.fileID)

  //             // 下载附件(临时文件)
  //             wx.cloud.downloadFile({
  //               fileID: res.fileID,
  //               success: sres => {
  //                 // get temp file path
  //                 console.log("下载附件成功", sres)

  //                 // 通过下载附件得到tempFilePath并读取
  //                 let fs = wx.getFileSystemManager()
  //                 let result = fs.readFileSync(sres.tempFilePath, "utf-8")
  //                 // 读取文件内容到result
  //                 console.log(result)
  //               },
  //               fail: sres => {
  //                 // handle error
  //                 console.log("下载附件失败", sres)
  //               },
  //             })
  //           },
  //           fail: res => {
  //             console.log("上传成绩附件失败", res)
  //           },
  //         })
  //       }
  //     })
  //   }
  // },

  //点击生成考号跳转到对应页面
  setOrder: function () {
    if (this.data.choosen_id == "") {
      wx.showToast({
        title: '请选择比赛',
        image: "/images/unhappy.png"
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/superCreateNumber/superCreateNumber?_id=' + that.data.choosen_id
      })
    }
  },

  // 点击修改跳转到比赛发布页面
  goToGameRelease: function (e) {
    // console.log(e)
    if (this.data.choosen_id == "") {
      wx.showToast({
        title: '请选择比赛',
        image: "/images/unhappy.png"
      })
    } else {
      let that = this
      // 将数组转为字符串传参
      var groupString = JSON.stringify(that.data.group);
      wx.navigateTo({
        url: '/pages/superGameRelease/superGameRelease?_id=' + that.data.choosen_id + '&title=' + that.data.title + '&group=' + groupString + "&host=" + that.data.host + "&code=" + that.data.code + "&fileID=" + that.data.fileID + "&sbd=" + that.data.sbd + "&sed=" + that.data.sed
      })
    }
  },

  // 点击删除从数据库删除对应比赛
  removeInfo: function (e) {
    if (this.data.choosen_id == "") {
      wx.showToast({
        title: '请选择比赛',
        image: "/images/unhappy.png"
      })
    } else {
      let that = this
      wx.showModal({
        title: '确认要删除该项吗？',
        success: function (res) {
          if (res.confirm) {
            console.log('点击确认回调')
            // console.log(e)
            wx.cloud.callFunction({
              name: "removeGameById",
              data: {
                _id: that.data.choosen_id
              },
              success: sres => {
                console.log("调用云函数删除比赛成功", sres)
                that.onShow()
                wx.showToast({
                  title: '删除比赛成功',
                })
              },
              fail: sres => {
                console.log("调用云函数删除比赛失败", sres)
              }
            })
          } else {
            console.log('点击取消回调')
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取比赛列表
    let that = this
    wx.cloud.callFunction({
      name: "getGamesList",
      success: res => {
        console.log("调用云函数获取游戏列表成功", res)
        that.setData({
          gamesList: res.result.data,
        })
      },
      fail: res => {
        console.log("调用云函数获取游戏列表失败", res)
      }
    })

    // 获取当前时间
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    var time = util.formatTime(new Date());
    // console.log(time)
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time: time
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    wx.cloud.callFunction({
      name: "getGamesList",
      success: res => {
        console.log("调用云函数获取游戏列表成功", res)
        that.setData({
          gamesList: res.result.data
        })
      },
      fail: res => {
        console.log("调用云函数获取游戏列表失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})